package com.zjitc.controller.admin;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.zjitc.model.*;
import com.zjitc.service.OrderService;
import com.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by IntelliJ IDEA
 *
 * @author: lhl
 * @date-Time: 2018/4/10 0010
 * @description:
 */
@Controller
@RequestMapping(value = "/admin/order")
public class AdminOrderControl {

    @Autowired
    private OrderService service;

    @RequestMapping(value = "/findOrders")
    @ResponseBody
    public ModelAndView findOrders(
            @RequestParam(name = "pageNum", defaultValue = "1", required = false) int pageNum,
            @RequestParam(name = "pageSize", defaultValue = "4", required = false) int pageSize) {
        ModelAndView mv = new ModelAndView(new InternalResourceView("/admin/order/list.jsp"));
        List<Order> orders = service.findOrders(pageNum, pageSize);
        PageInfo info = new PageInfo(orders);
        mv.addObject("page", info);
        return mv;
    }

    @RequestMapping(value = "/orderItems")
    @ResponseBody
    public List<OrderItem> findOrderItems(String oid) {
        List<OrderItem> items = service.findOrderItemsByOid(oid);
        System.out.println(items);
        return items;
    }

    @RequestMapping(value = "/findBuyerByOid")
    @ResponseBody
    public Order findBuyerByOid(String oid) {
        Order order = service.findBuyerByOid(oid);
        if (order != null) {
            return order;
        } else {
            return null;
        }
    }
}
